<?php
namespace Admin\Controller;
// use Shop\Api\ShopApi;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class AttrController extends AdminController {
   public function index(){
   	   $obj = M();
   	   $sql = 'SELECT
		s_attrkey.id,
		s_attrkey.kname,
		s_attrkey.gid,
		s_attrkey.ctime,
		s_attrkey.status,
		s_goodshop.`name`
		FROM
		s_attrkey ,
		s_goodshop
		WHERE
		s_attrkey.gid = s_goodshop.id';
   	    $list=$obj->query($sql);
   	    $this->assign('_list',$list);
   	    $this->meta_title = '商品属性';
        $this->display();
   }

   //属性类别新增
   public function add(){
   	$id = I('get.id');

   	if(IS_POST){
   		$attr = D('Attrkey');
   		$res = $attr->create();
   		if($res){
   			if($attr->add()){
   				$this->success('新增成功');
   			}else{
   				$this->error('新增失败');
   			}
   		}else{
   			$this->error($attr->getError());
   		}
   	}else{
   		$this->assign('id',$id);
   		$this->meta_title = '新增属性';
   		$this->display();
   	}

   }
  


   //属性类别修改
   public function update(){
    $id = I('get.id');
    $attr = D('Attrkey');
    if(IS_POST){
      
      
      $res = $attr->create();
      // var_dump($res);die;
      if($res){
        if($attr->save()){
          $this->success('修改属性成功');
        }else{
          $this->error('修改属性失败');
        }
      }else{
        $this->error($attr->getError());
      }
    }else{
      $info = $id ? $attr->info($id) : '';
      $this->assign('info',   $info);
      $res = M('attrkey')->find("$id");
      $this->assign('res',$res);
      $this->meta_title = '修改属性';
      $this->display('add');
    }

   }
   //属性值修改
   public function edit(){
    $id = I('get.id');
    $attr = D('Attrvalue');
    if(IS_POST){
      
      
      $res = $attr->create();
      // var_dump($res);die;
      if($res){
        if($attr->save()){
          $this->success('修改属性值成功');
        }else{
          $this->error('修改属性值失败');
        }
      }else{
        $this->error($attr->getError());
      }
    }else{
      
      $res = M('attrval')->find("$id");
      $this->assign('res',$res);
      $this->meta_title = '修改属性值';
      $this->display('edit');
    }

   }
  //属性值列表
   public function Attrindex(){
    $map['gid'] = I('get.gid');
    $map['kid'] =  $kid = I('get.kid');
    $title       =   I('vname');
    $map['status']  =   array('egt',0);
    if(is_numeric($vname)){
      $map['id|vname']=   array(intval($vname),array('like','%'.$vname.'%'),'_multi'=>true);
    }else{
      $map['vname']    =   array('like', '%'.(string)$vname.'%');
    }
    
$list=M()->field('a.id,a.kid,a.gid,a.vname,a.ctime,a.status,b.name,c.kname')->table('s_attrval as a,s_goodshop as b,s_attrkey as c
  ')->where("a.kid=c.id and a.gid=b.id and a.kid= $kid ")->select();
     $res = M('attrkey')->find($kid); 
    $this->assign('res', $res);
    $this->assign('_list', $list);
    $this->assign('map',$map);
    $this->meta_title = '属性值列表';
    $this->display();
  }
 //属性值添加
   public function Attrvalue(){
   	$map['gid'] = I('get.gid');
   	$map['kid'] = $id = I('get.kid');
    $attr = D('Attrvalue');
   	if(IS_POST){
   		
   		$res = $attr->create();
   		 // var_dump($res);die;
   		if($res){
   			if($attr->add()){
   				$this->success('新增属性值成功');
   			}else{
   				$this->error('新增属性值失败');
   			}
   		}else{
   			$this->error($attr->getError());
   		}

   	}else{
      $res = M('attrkey')->find($id);
      $this->assign('res',$res);
   		$this->assign('map',$map);
   		$this->meta_title = '新增属性值';
   		$this->display();
   	}

   }

   //属性类别删除
   public function del(){
    $cate_id = I('id');
    if(empty($cate_id)){
      $this->error('参数错误!');
    }

    $tt = M('attrval')->where(array('kid'=>$cate_id))->select();
    $re = M('attrkey')->delete($cate_id);
    if( $re !== false){
      if(!empty($tt)){
        $rr = M('attrval')->where(array('kid'=>$cate_id))->delete();
        if($rr){

          $this->success('删除属性和值成功！') ;
        }else{
          $this->error('删除属性成功其值失败！');
        }

      }else{
        echo '没有属性值';
      }

    }else{
      $this->error('删除属性失败！');
    }
  }


   //属性值删除
   public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }
       
        $re = M('attrval')->where(array('id'=>$cate_id))->delete();
        // $res = M('attrkey')->delete($cate_id);
        
        if( $re !== false){
            //记录行为
            action_log('update_attrkey', 'attrkey', $cate_id, UID);
            $this->success('删除属性成功！');
        }else{
            $this->error('删除属性失败！');
        }
    }

    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbidfriend':
                $this->forbid('Friend', $map );
                break;
            case 'resumefriend':
                $this->resume('Friend', $map );
                break;
            case 'deletefriend':
                $this->delete('Friend', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

    



}
 

?>